﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Basic
{
    /// <summary>
    /// 一个简单的Cache对象 通过字典实现
    /// </summary>
    public class SimpleCacheObject<TKey, TValue>
    {
        #region Fields
        /// <summary>
        /// 内部的Dictionary容器
        /// </summary>
        private readonly Dictionary<TKey, TValue> _dictionary = new Dictionary<TKey, TValue>();



        /// <summary>
        /// 用于控制并发同步访问的RW锁对象
        /// </summary>
        private readonly ReaderWriterLock _rwLock = new ReaderWriterLock();//多读单写锁

        /// <summary>
        /// 一个TimeSpan,用于指定超时时间
        /// </summary>
        private readonly TimeSpan _lockTimeOut = TimeSpan.FromMilliseconds(100);
        #endregion

        #region Methods

        /// <summary>
        /// 将指定的键和值添加到字典中
        /// Exception: ArgumentException - Dictionary中已存在具有相同键的元素
        /// </summary>
        /// <param name="key">要添加的元素的键</param>
        /// <param name="value">添加的元素的值，对于引用类型，该值可以为空引用</param>
        public void Add(TKey key, TValue value)
        {
            bool isExisting = false;
            _rwLock.AcquireReaderLock(_lockTimeOut);
            try
            {
                if (!_dictionary.Keys.Contains(key)) _dictionary.Add(key, value);
                else { isExisting = true; }
            }
            finally { _rwLock.ReleaseReaderLock(); }
            if (isExisting) throw new IndexOutOfRangeException();
        }

        /// <summary>
        /// 获取与指定的键对应的值
        /// Exception: ArgumentException -Dictionary中不存在该键
        /// </summary>
        /// <param name="key">要获取的键</param>
        /// <param name="value">当此方法返回时，如果找到该键，便会返回与键对应的值
        /// 否则，则会返回value参数类型的默认值，该参数未经初始化即被传递</param>
        /// <returns>如果Dictionary包含指定键的元素，则为true，否则为false</returns>
        public bool TryGetValue(TKey key, out TValue value)
        {
            _rwLock.AcquireReaderLock(_lockTimeOut);
            bool result;
            try
            {
                result = _dictionary.TryGetValue(key, out value);
            }
            finally { _rwLock.ReleaseReaderLock(); }
            return result;
        }

        /// <summary>
        /// 从Dictionary中移除所有的键和值
        /// </summary>
        public void Clear()
        {
            if (_dictionary.Count <= 0) return;
            _rwLock.AcquireWriterLock(_lockTimeOut);
            try
            {
                _dictionary.Clear();
            }
            finally { _rwLock.ReleaseWriterLock(); }
        }

        /// <summary>
        /// 根据key删除原色
        /// Exception: ArgumentException -Dictionary中不存在该键
        /// </summary>
        /// <param name="key">Key</param>
        /// <returns>是否删除成功</returns>
        public bool Delete(TKey key)
        {
            _rwLock.AcquireWriterLock(_lockTimeOut);
            bool result;
            try
            {
                result = _dictionary.Remove(key);
            }
            finally { _rwLock.ReleaseWriterLock(); }
            return result;
        }

        /// <summary>
        /// 确定Dictinary是否包含指定的键
        /// </summary>
        /// <param name="key">要在Dictionary中定位的键</param>
        /// <returns>如果Dictionary包含具有指定键的元素，则为true，否则为false</returns>
        public bool ContainsKey(TKey key)
        {
            if (_dictionary.Count <= 0) return false;
            bool result;
            _rwLock.AcquireReaderLock(_lockTimeOut);
            try
            {
                result = _dictionary.ContainsKey(key);
            }
            finally
            {
                _rwLock.ReleaseReaderLock();
            }
            return result;
        }

        /// <summary>
        /// 获取包含在Dictionary中的键/值对的数量
        /// </summary>
        public int Count => _dictionary.Count;


        #endregion
    }


}
